package com.itliupeng.stock.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.itliupeng.stock.pojo.domain.children;
import com.itliupeng.stock.pojo.entity.SysUser;
import com.itliupeng.stock.service.UserService;
import com.itliupeng.stock.vo.req.LoginReqVo;
import com.itliupeng.stock.pojo.domain.queryUser;
import com.itliupeng.stock.pojo.domain.UserInfoVo;
import com.itliupeng.stock.vo.resp.*;
import io.lettuce.core.api.push.PushListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Description:用户接口
 * Author:lp
 * Date:2025-04-07
 * Time:10:08
 */
/*这里复习到：RestController注解
是controller和responsebody两个注解的缩写
注意面试提问：
@RequestBody：接口json数据转化成实体对象-反序列化
@ResponseBody：将实体类对象转json格式字符串：序列化
*
* */
@Api(value = "/api", tags = {"用户接口"})
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据用户id查询用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userName", value = "", required = true)
    })
    @ApiOperation(value = "根据用户名查询用户信息", notes = "根据用户id查询用户信息", httpMethod = "GET")
    @GetMapping("/user/{userName}")//这里值得注意的@pathVariable注解默认把路径中的变量赋值给参数，但是前提是首字母缩写得对应的上
    public SysUser getUserByUserName(@PathVariable String userName){
        //这里还需要加以描述：需要对传递回去的参数做描述，所以在System中做描述
        return userService.findByUserName(userName);
    }


    /**
     * 执行登录操作
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "LoginReqVo", name = "loginReqVo", value = "", required = true)
    })
    @ApiOperation(value = "执行登录操作", notes = "执行登录操作", httpMethod = "POST")
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo loginReqVo){
        R<LoginRespVo> login = userService.login(loginReqVo);
        return login;
    }

    /*
    *
    *
    * */

    @ApiOperation(value = "获取校验码", notes = "获取校验码", httpMethod = "GET")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode(){
        return userService.getCaptchaCode();
    }


    /*
    * 多条件综合查询用户分页信息
    * */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "queryUser", name = "queryUs", value = "", required = true)
    })
    @ApiOperation(value = "多条件综合查询用户分页信息", notes = "多条件综合查询用户分页信息", httpMethod = "POST")
    @PostMapping("/users")
    public R<PageResult<SysUser>> getUserPage(@RequestBody queryUser queryUs){
        return userService.getUserPage(queryUs);
    }


    /**
     * 创建用户
     * @param sysUser
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysUser", name = "sysUser", value = "", required = true)
    })
    @ApiOperation(value = "创建用户", notes = "创建用户", httpMethod = "POST")
    @PostMapping("/user")
    public R<ResponseCode> addUser(@RequestBody SysUser sysUser){
        return  userService.addUser(sysUser);
    }

    /**
     * 获取用户对应的信息
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "获取用户对应的信息", notes = "获取用户对应的信息", httpMethod = "GET")
    @GetMapping("/user/roles/{userId}")
    public R<Map<String, List>> getUserRole(@PathVariable String userId){
        return userService.getUserRole(userId);
    }

    /**
     * 用户角色更新
     * @param req
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Map<String, Object>", name = "req", value = "", required = true)
    })
    @ApiOperation(value = "用户角色更新", notes = "用户角色更新", httpMethod = "PUT")
    @PutMapping("/user/roles")
    public R<ResponseCode> updateUserRoles(@RequestBody Map<String,Object> req){
        System.out.println(req.toString());
        return userService.updateUserRoles(req);
    }

    /**
     * 删除用户信息
     * @param UserList
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "List<Long>", name = "UserList", value = "", required = true)
    })
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息", httpMethod = "DELETE")
    @DeleteMapping("/user")
    public R<ResponseCode> deleteUser(@RequestBody List<Long> UserList){
        return userService.deleteUser(UserList);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "查询用户信息", notes = "", httpMethod = "GET")
    @GetMapping("/user/info/{userId}")
    public R<SysUser> getUserInfo(@PathVariable String userId){
        return userService.getUserInfo(userId);
    }

    @PutMapping("/user")
    public R<ResponseCode> updateUserInfo(@RequestBody UserInfoVo userInfoVo){
        return userService.updateUserInfo(userInfoVo);
    }

    @PostMapping("/roles")
    public R<PageResult> getRolePages(@RequestBody Map<String, Integer> pageInfo){
        return userService.getRolePages(pageInfo);
    }

    @GetMapping("/permissions/tree/all")
    public R<List<children>> getPermissionsTree(){
        return userService.getPermissionsTree();
    }

    @PostMapping("/role")
    public R<ResponseCode>  addRoleAndPer(@RequestBody Map<String,Object> addRole) throws JsonProcessingException {
        return userService.addRoleAndPer(addRole);
    }

    @GetMapping("/role/{roleId}")
    public R<List<String>> getRolePers(@PathVariable String roleId){
        return userService.getRolePers(roleId);
    }

    @PutMapping("/role")
    public R<ResponseCode> updateRoleAndPer(@RequestBody Map<String,Object> updateRole) throws JsonProcessingException {
        return userService.updateRoleAndPer(updateRole);
    }

    @DeleteMapping("/role/{roleId}")
    public R<ResponseCode> deleteRoleAndPer(@PathVariable String roleId){
        return userService.deleteRoleAndPer(roleId);
    }

    @PostMapping("/role/{roleId}/{status}")
    public R<ResponseCode> updateRoleStatus(@PathVariable Long roleId,@PathVariable Long status){
        return userService.updateRoleStatus(roleId,status);
    }
}














